package com.pro.controller;


import com.pro.pojo.JsonData;
import com.pro.utils.FastDFSClientUtil;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * ClassName:UploadFileController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2021/11/6 14:48
 * @Author: ZPT
 */
@RestController
@RequestMapping("/upload")
public class UploadFilesController {
    @Autowired
    private FastDFSClientUtil dfsClient;
    /**
     *
     * @author ZPT
     * @date 2021/11/20 17:51
     * @content :上传文件
     */
    @RequestMapping("/uploadFile")
    public JsonData fdfsUpload(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {
        String resAccessUrl =null;
        try {
            for (MultipartFile file : files) {
                String fileUrl = dfsClient.uploadFile(file);
                //路径
                resAccessUrl = dfsClient.getResAccessUrl(fileUrl);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }
    /**
     *
     * @author ZPT
     * @date 2021/11/20 17:51
     * @content :文件的下载
     */
    @RequestMapping("/download")
    public void download(String filePath , HttpServletRequest request , HttpServletResponse response) throws IOException {

        //    group1/M00/00/00/wKgIZVzZEF2ATP08ABC9j8AnNSs744.jpg
        String[] paths = filePath.split("/");
        String groupName = null ;
        for (String item : paths) {
            if (item.indexOf("group") != -1) {
                groupName = item;
                break ;
            }
        }
        String path = filePath.substring(filePath.indexOf(groupName) + groupName.length() + 1, filePath.length());
        InputStream input = dfsClient.download(groupName, path);

        //根据文件名获取 MIME 类型
        String fileName = paths[paths.length-1] ;
        System.out.println("fileName :" + fileName); // wKgIZVzZEF2ATP08ABC9j8AnNSs744.jpg
        String contentType = request.getServletContext().getMimeType(fileName);
        String contentDisposition = "attachment;filename=" + fileName;

        // 设置头
        response.setHeader("Content-Type",contentType);
        response.setHeader("Content-Disposition",contentDisposition);

        // 获取绑定了客户端的流
        ServletOutputStream output = response.getOutputStream();

        // 把输入流中的数据写入到输出流中
        IOUtils.copy(input,output);
        input.close();

    }
}
